import type { HttpContext } from '@adonisjs/core/http'
import BaseController from '#common/base_controller'
import StockOrderModel from '#models/stock_order'
import StockOutService from '#services/stock_out_service'
import { createStockOutValidator } from '#validators/stock'

export default class StockOutController extends BaseController {
  async list(ctx: HttpContext) {
    return this.generatePageData(ctx, StockOrderModel)
  }
  async create(ctx: HttpContext) {
    const data = await ctx.request.validateUsing(createStockOutValidator)
    return StockOutService.create('CK', data)
  }

  async detail({ params }: HttpContext) {
    const stockInInfo = await StockOrderModel.findOrFail(params.orderId)
    await stockInInfo.load('stockOutDetail')
    return stockInInfo
  }

  async update(ctx: HttpContext) {
    const data = await ctx.request.validateUsing(createStockOutValidator)
    const orderId = ctx.request.param('orderId')
    return await StockOutService.update(orderId, data)
  }

  async delete(ctx: HttpContext) {
    const orderId = ctx.request.param('orderId')
    return await StockOutService.delete(orderId)
  }
}
